﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Win32;

namespace ChangeRegionalSettings
{
    public class RegistryChanger
    {
        //Using locale ID right now because locale name doesnt exist in WinXP
        static string WindowsLocaleId = "00000425";

        public static void ChangeCurrencyFromEuroToKr()
        {
            ChangeCurrency("kr");
        }

        public static void ChangeCurrencyFromKrToEuro()
        {
            ChangeCurrency("€");
        }

        /// <summary>
        /// Changes the currency from kr to euro in registry for all users.
        /// </summary>
        internal static void ChangeCurrency(string newCurrencySymbol)
        {
            string regKeyPattern = @"{0}\{1}\Control Panel\International";
            string locale = "Locale";
            string currencyName = "sCurrency";
            var names = Registry.Users.GetSubKeyNames();

            foreach (var userKey in names)
            {
                //Skip any subkey under users with ending "Classes"
                if (userKey.EndsWith("Classes") || string.IsNullOrEmpty(userKey))
                    continue;

                string internationalRegkey = string.Format(regKeyPattern, Registry.Users.Name, userKey);

                //Get the Locale value for this user
                var localeValue = Registry.GetValue(internationalRegkey, locale, null) as string;

                //00000425 is et-EE locale. Windows XP doesnt have LocaleName key.
                //Continue if the user has et-EE locale
                if (localeValue == null || !localeValue.Equals(WindowsLocaleId))
                    continue;

                Console.WriteLine("Starting to update user with SID: {0}", userKey);

                //Get currency value
                var currencyValue = Registry.GetValue(internationalRegkey, currencyName, null);

                if (currencyValue == null)
                    continue;

                Console.WriteLine("Current currency value: " + currencyValue);

                //Update registry
                Registry.SetValue(internationalRegkey, currencyName, newCurrencySymbol);

                currencyValue = Registry.GetValue(internationalRegkey, currencyName, null);
                Console.WriteLine("Changed currency value: " + currencyValue);
            }
        }
    }
}
